package org.linlinjava.litemall.wx.service;

import static org.linlinjava.litemall.wx.util.WxResponseCode.COMMON_DETAIL_IS_NULL;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.linlinjava.litemall.core.util.ConvertUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.service.QuoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.hutool.core.map.MapUtil;

@Service
public class WxQuoteService {
	@Autowired
	private QuoteService quoteService;

	public Object list(Integer userId, Byte type, Integer page, Integer limit, String sort, String order,
			String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		List<String> querys = Lists.newArrayList();
		// 状态切换 TODO
		switch (type) {
		case 0:// 全部
			querys.add(" (fp.status = 2 or fp.status = 4 or fp.status = 5) ");
			querys.add(" (fpi.check_status = 1 or fpi.check_status = 2 or fpi.check_status = 3) ");
			querys.add(" (fpi.status = 2 or fpi.status = 3) ");
			querys.add(" qp.status = 1 ");
			break;
		case 1:// 送检中
			querys.add(" (fp.status = 2 or fp.status = 4 or fp.status = 5) ");
			querys.add(" fpi.check_status = 1 ");
			querys.add(" (fpi.status = 2) ");
			querys.add(" qp.status = 1 ");
			break;
		case 2:// 检验合格
			querys.add(" (fp.status = 2 or fp.status = 4 or fp.status = 5) ");
			querys.add(" fpi.check_status = 2 ");
			querys.add(" (fpi.status = 3) ");
			querys.add(" qp.status = 1 ");
			break;
		case 3:// 检验不合格
			querys.add(" (fp.status = 2 or fp.status = 4 or fp.status = 5) ");
			querys.add(" fpi.check_status = 3 ");
			querys.add(" (fpi.status = 3) ");
			querys.add(" qp.status = 1 ");
			break;
		}

		if (!StringUtils.isEmpty(searchValue)) {
			querys.add(
					" CONCAT(IFNULL(s.company_name,''),IFNULL(h.herb_name,''),IFNULL(h.code,''),IFNULL(fp.contact_name,''),IFNULL(fp.contact_tel,'')) LIKE '%"
							+ searchValue + "%' ");
		}
		querys.add(" u.id = " + userId + " ");
		querys.add(" fp.deleted = 0 ");
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		String orderByClause = " fpi.herb_id,fpi.specifications,fp.publish_dt desc ";

		PageHelper.startPage(page, limit);
		List<Map> mapList = quoteService.getQuotes(query, orderByClause);
		Page<Map> list = (Page) mapList;

		Map<String, Object> data = Maps.newHashMap();
		data.put("list", ConvertUtils.sourceToCamelCaseMap(list));
		data.put("total", list.getTotal());
		data.put("page", list.getPageNum());
		data.put("limit", list.getPageSize());
		data.put("pages", list.getPages());
		return ResponseUtil.ok(data);
	}

	public Object detail(Integer userId, Integer id, Integer itemId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		Map<String, Object> data = Maps.newHashMap();
		List<String> querys = Lists.newArrayList();
		querys.add(" u.id = " + userId + " ");
		querys.add(" fp.id = " + id + " ");
		querys.add(" fpi.id = " + itemId + " ");
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		List<Map> mapList = quoteService.getQuotes(query, null);
		if (CollectionUtils.isNotEmpty(mapList)) {
			data.put("purchase", MapUtil.toCamelCaseMap(mapList.get(0)));
		} else {
			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "采购详情不存在或已被删除");
		}
		return ResponseUtil.ok(data);
	}
}